package com.wys.read.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.wys.read.entity.Article;
import com.wys.read.exception.FrameErrorCodeEnums;
import com.wys.read.service.ArticleService;
import com.wys.read.utils.ResultUtil;
import com.wys.read.vo.CategoryVO;
import com.wys.read.vo.ResultVO;
import com.wys.read.vo.TitleVO;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 文章控制类
 * @author wys
 * created in 22:47 2019/1/5
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 增加文章
     * @param article 文章
     * @return
     */
    @PostMapping("/add")
    public ResultVO articleAdd(@RequestBody Article article) {

        Boolean result = articleService.articleAdd(article);

        return ResultUtil.success( result ? "增加成功" : "增加失败");
    }

    /**
     * 删除文章
     * @param articleId 文章ID
     * @param category 分类
     * @return
     */
    @PostMapping("/delete")
    public ResultVO articleDelete(@RequestParam Integer articleId, @RequestParam String category) {
        if (ObjectUtil.isNull(articleId) || StrUtil.isBlank(category)) {
            return ResultUtil.failure(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }

        int result = articleService.articleDelete(articleId, category);

        return ResultUtil.success( result == 1? "文章删除成功" : "文章不存在" );
    }

    /**
     * 更新文章
     * @param article 文章
     * @return
     */
    @PostMapping("/update")
    public ResultVO articleUpdate(@RequestBody Article article) {
        if (ObjectUtil.isNull(article)) {
            return ResultUtil.failure(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }

        int result = articleService.articleUpdate(article);

        return ResultUtil.success(result == 1 ? "文章更新成功" : "文章不存在");
    }

    @GetMapping("/test")
    public ResultVO test() {

        return ResultUtil.success();
    }

    /**
     * 通过文章ID获得一篇文章
     * @param articleId 文章ID
     * @return
     */
    @GetMapping("/get/{articleId}")
    public ResultVO getArticle(@PathVariable Integer articleId) {
        if (ObjectUtil.isNull(articleId)) {
            return ResultUtil.failure(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }
        Article article = articleService.getArticle(articleId);
        return ResultUtil.success(ObjectUtil.isNotNull(article) ? article : "没有这篇文章");
    }

    /**
     * 分页获取
     * @param page 页数
     * @param limit 一页条数
     * @return
     */
    @GetMapping("/page/{page}/{limit}")
    public ResultVO getPageArticle(@PathVariable int page, @PathVariable int limit) {
        if (page < 0 || limit < 0) {
            return ResultUtil.failure(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }

        PageQuery<Article> articlePageQuery = articleService.getPageArticle(page, limit);

        log.info("=== {}", new Date());
        return ResultUtil.success((int) articlePageQuery.getTotalPage(), articlePageQuery.getList());
    }

    /**
     * 获得最新的5篇文章
     * @return
     */
    @GetMapping("/new")
    public ResultVO getNewArticles() {

        List<Article> newArticles = articleService.getNewArticles();
        return ResultUtil.success(newArticles.isEmpty() ? "暂没有文章" : newArticles);
    }

    /**
     * 获取文章  文章查看  通过分类类型
     * @param category  分类类型
     * @return 文章
     */
    @GetMapping("read/{category}")
    public ResultVO getArticles(@PathVariable String category) {

        List<Article> articles = articleService.getArticles(category);

        return ResultUtil.success(articles.isEmpty() ? "暂时没有这个类型的文章" : articles);
    }

    /**
     * 最新更新
     * @return
     */
    @GetMapping("/update")
    public ResultVO getUpdateArticles() {

        List<TitleVO> updateArticles = articleService.getUpdateArticles();

        return ResultUtil.success(updateArticles.isEmpty() ? "暂没有文章" : updateArticles);
    }

    /**
     * 获取分类  排序：10条 最近更新的
     * @return
     */
    @GetMapping("/category")
    public ResultVO getCategory() {

        List<CategoryVO> categoryVOS = articleService.getCategory();
        return ResultUtil.success(categoryVOS.isEmpty() ? "暂没有文章" : categoryVOS);
    }

    /**
     * 获取文章的标签
     * @return 标签数组
     */
    @GetMapping("/label")
    public ResultVO getLable() {



        return ResultUtil.success();
    }

}
